package com.lp.multi.juc.collection;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

/**
 * @author: lp
 * @create: 2021-06-27 16:56
 * @description: 7龙珠召唤神龙(加法计数器)
 */
public class CyclicBarrierTest {
    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("7颗龙珠已经收集完成，召唤神龙成功!");
        });
        System.out.println("传说集齐7颗龙珠可以召唤神龙");
        for (int i = 0; i < 7;i++) {
            final int number = i+1;
            new Thread(()->{
                System.out.println("收集到第"+number+"颗龙珠！");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
